package com.aki.ai_property.controller;

import com.aki.ai_property.dto.HouseExportDto;
import com.aki.ai_property.dto.SubscriptionExportDto;
import com.aki.ai_property.dto.SellHouseDto;
import com.aki.ai_property.entity.House;
import com.aki.ai_property.service.intf.HouseService;
import com.aki.ai_property.util.FileUtils;
import com.aki.ai_property.vo.PageBean;
import com.aki.ai_property.vo.Result;
import com.alibaba.excel.EasyExcel;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Author:WuChuangChuang
 * Date:2022/8/27 15:20
 */
@RestController
@RequestMapping("/api/house")
public class HouseController {
    @Autowired
    private HouseService service;

    /** 新增 */
    @PostMapping("/save.do")
    public Result save(House house){
        return service.save(house);
    }

    /** 修改 */
    @PostMapping("/update.do")
    public Result update(House house){
        return service.update(house);
    }

    /** 删除 */
    @PostMapping("/delete.do")
    public Result delete(int id){
        return service.delete(id);
    }

    /** 查询 */
    @GetMapping("/all.do")
    public PageBean selectPage(String houseType, int page, int limit){
        return service.selectPage(houseType,page,limit);
    }

    /** 查询全部*/
    @GetMapping("/allhouse.do")
    public Result all(){
        return service.all();
    }

    @PostMapping("/loc.do")
    public Result loc(MultipartFile file, HttpServletRequest request) throws IOException {
        //1.验证文件是否存在
        if(!file.isEmpty()){
            //2.获取上传文件名并完成重命名
            String fn= FileUtils.reName(file.getOriginalFilename());
            //3.获取上传的文件内容
            byte[] data=file.getBytes();
            //4.创建文件存储的文件夹
            File dir=new File(request.getServletContext().getRealPath("/upload"));
            if(!dir.exists()){
                dir.mkdirs();
            }
            //5.存储
            IOUtils.write(data,new FileOutputStream(new File(dir,fn)));
            //6.返回
            return Result.ok("/upload/"+fn);
        }else {
            return Result.fail();
        }

    }

    /**信息导出*/
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition","attachment;filename=house-"+System.currentTimeMillis()+".xlsx");
        //2.查询学员数据
        List<HouseExportDto> list=service.allHouse();
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(),HouseExportDto.class).sheet(System.currentTimeMillis()+"").doWrite(list);
    }
//    @GetMapping("all1.do")
//    public Result all(){
//        return service.all();
//
//    }
}
